﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class SchemasDocument : Document
    {
        #region Public
        internal SchemasDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Schemas;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "schemas.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Schemas"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.propertyColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.valueColumn { width: 20%; }");

                Boolean evenRow = true;
                #region Schemas
                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Schemas</h2>");
                
                AddBodyLine(@"<table>");

                AddBodyLine(@"<tr><th>Name</th><th>Owner</th><th>Description</th></tr>");
                AddBodyLine(@"<colgroup><col class=""propertyColumn""><col class=""valueColumn"">" +
                    @"<col class=""descriptionColumn""></colgroup>");

                var schemas = (from schema in _database.Schemas
                               orderby schema.Name
                               select schema);

                evenRow = true;
                foreach (SqlServer.Schema schema in schemas)
                {
                    StringBuilder sb = new StringBuilder();

                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(schema.Name) + @"</td>");
                    
                    if (schema.Owner == null)
                        sb.Append(@"<td>" + ScrubHtml(schema.Name) + @"</td>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(schema.Owner.Name) + @"</td>");

                    if (schema.Description == "")
                        sb.Append(@"<td>&nbsp;</td>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(schema.Description) + @"</td>");

                    sb.Append(@"</tr>");

                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
